package com.jule.miaozhuapc.controller;

import com.jule.miaozhuapc.ano.Auth;
import com.jule.miaozhuapc.ano.TokenUser;
import com.jule.miaozhuapc.domain.WawaGameRecord;
import com.jule.miaozhuapc.domain.WawaUserLocation;
import com.jule.miaozhuapc.dto.*;
import com.jule.miaozhuapc.service.*;
import com.jule.miaozhuapc.util.Const;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jule.miaozhuapc.domain.WawaUser;
import com.jule.miaozhuapc.req.dto.Location;

import java.util.List;

@Controller
@RequestMapping( value = Const.WAWA_HTTP_URL_V1+ "/user")
public class UserController {
	private Logger logger = Logger.getLogger(UserController.class);

	@Autowired
	private UserService userService;
	@Autowired
	private CheckTodayService checkTodayService;
	@Autowired
	private UserLocationService userLocationService;
	@Autowired
	private GameRecordService gameRecordService;
	@Autowired
	private PayConfigService payConfigService;
	
	// responseBody将返回值转化为json格式响应到客户端
	// requestBody将请求数据转化为json对象
//	@RequestMapping(value="/user/getbyname/{username}",method=RequestMethod.GET)
//	public @ResponseBody WawaUser CheckUsername(
//			@PathVariable(name="username") String username,
//			@RequestParam(name="nick",required=false) String nick) {
//		WawaUser user = userService.findByUsername(username);
//		UserDto dto = new UserDto();
//		dto.setWxGender(user.getWxGender());
//		return user;
//	}
//
//	@RequestMapping(value="/user/users/{username}",method=RequestMethod.POST)
//	public @ResponseBody UserDto CheckUsername(@RequestBody UserForm form) {
//
//		return null;
//	}
//	@RequestMapping(value="/user/users",method=RequestMethod.GET, produces="application/json; charset=UTF-8")
//	@ResponseBody
//	public  WsUtil CheckUsername() {
//		WsUtil rst  = new WsUtil();
//		UserDto dto = new UserDto();
//		dto.setWxGender("男");
//		rst.setBody(dto);
//		return rst;
//	}

	/**
	 *  用户资料
	 */
	@Auth
	@RequestMapping(value="/getUserDetail",method=RequestMethod.GET)
	public @ResponseBody WsUtil getUserDetail(
			@TokenUser WawaUser user) {

		WsUtil ws  = new WsUtil();
		UserDto userDetail = userService.getUserDetail(user.getId());

		ws.setBody(userDetail);
		return  ws;
	}
	
	// 抓取记录接口 入参：userId、pageNum、pageSize 出参：数组，每项为一个字典，包含预览图地址、娃娃名、时间（字符串，格式 yyyy/MM/dd hh:mm:ss）
	@Auth
	@RequestMapping(value="/getGameRecord",method=RequestMethod.GET)
	public @ResponseBody WsUtil getGameRecord(
			@TokenUser WawaUser user ,
			@RequestParam(name="type",required=false) Boolean type,
			@RequestParam(name="size",required=false) Integer size,
			@RequestParam(name="num",required=false) Integer num ) {

		type = type != null ? type : true;
		num = num != null ? num : 1;
		size = size != null ? size: 10;

		List<GameRecordDto> gameRecordDtos = gameRecordService.findAllByUserId(user.getId(), type, num , size);

		logger.info(new JSONArray(gameRecordDtos));
		WsUtil ws = new WsUtil();
		ws.setBody(gameRecordDtos);
		return ws;
	}

	// 录入收货地址
	@Auth
	@RequestMapping(value="/setLocation",method=RequestMethod.POST)
	public @ResponseBody WsUtil setLocation(@RequestBody Location location, @TokenUser WawaUser user) {
		location.setUserId(user.getId());
		userLocationService.updateLocation(location);
		return new WsUtil();
	}

	//获取收货地址
	@Auth
	@RequestMapping(value="/getLocation",method=RequestMethod.GET)
	public @ResponseBody WsUtil getLocation(@TokenUser WawaUser user) {
		// TODO
		WawaUserLocation wawaUserLocation = userLocationService.findByUserId(user.getId());
		WsUtil ws = new WsUtil();
		ws.setBody(wawaUserLocation);
		return ws;
	}

	//获取充值比例
	@RequestMapping(value="/getPayConfig",method=RequestMethod.GET)
	public @ResponseBody WsUtil getPayConfig() {
		List<PayConfigDto> payConfigDtos = payConfigService.findAll();
		WsUtil ws = new WsUtil();
		ws.setBody(payConfigDtos);
		return ws;
	}

}